ig.module(
	'game.entities.base.dictionary'
)
.requires(
	'game.entities.base.entityBase'
)

.defines(function()
{
    Dictionary = EntityEntityBase.extend(
    {
	font: new ig.Font( 'media/fonts/arialRounded.png' ),
        redOverlay: new ig.Image('media/images/red.png'),
        blueOverlay: new ig.Image('media/images/blue.png'),
        greenOverlay: new ig.Image('media/images/green.png'),
        yellowOverlay: new ig.Image('media/images/yellow.png'),
        pinkOverlay: new ig.Image('media/images/pink.png'),
        orangeOverlay: new ig.Image('media/images/orange.png'),
        
        ok: new ig.Image('media/images/ok.png'),
        
	collides: ig.Entity.COLLIDES.NONE,
        
        words: null,
        columns: 12,
        lines: 12,
        randomLetters: null,
        
        letterSize: {x:40, y:40},

        build: function()
        {
            this.words = new Array();

            var i = 0;
            
            this.words[i] = new Word();
            this.words[i].setWord('galinha');
            this.words[i].overlayType = 'red';
            this.words[i].line = 0;
            this.words[i].column = 1;
            this.words[i].vertical = true;
            i++;
            
            this.words[i] = new Word();
            this.words[i].setWord('pato');
            this.words[i].overlayType = 'orange';            
            this.words[i].line = 1;
            this.words[i].column = 0;
            i++;
            
            this.words[i] = new Word();
            this.words[i].setWord('biblioteca');
            this.words[i].overlayType = 'pink';            
            this.words[i].line = 2;
            this.words[i].column = 2;
            i++;
            
            this.words[i] = new Word();
            this.words[i].setWord('caramujo');
            this.words[i].overlayType = 'green';            
            this.words[i].line = 3;
            this.words[i].column = 3;
            this.words[i].vertical = true;
            i++;
            
            this.words[i] = new Word();
            this.words[i].setWord('tirania');
            this.words[i].overlayType = 'yellow';            
            this.words[i].line = 5;
            this.words[i].column = 5;
            this.words[i].vertical = false;
            i++;
            
            this.words[i] = new Word();
            this.words[i].setWord('papagaio');
            this.words[i].overlayType = 'blue';            
            this.words[i].line = 11;
            this.words[i].column = 4;
            this.words[i].vertical = false;
            
            this.randomLetters = new Array;

            var alphabet = 'abcdefghijklmnopqrstuvwxyz';
            
            for (var i = 0; i < this.columns; ++i)
            {
                this.randomLetters[i] = new Array();
                for (var j = 0; j < this.lines; ++ j)
                {
                    for (var k = 0; k < this.words.length; ++k)
                    {
                        if (this.words[k].isLetter(i, j) == true)
                        {
                            this.randomLetters[i][j] = '#';
                        }
                        else
                        {
                            if (this.randomLetters[i][j] != '#')
                            {
                                var number = Math.floor((Math.random() * 26) % 26);
                                this.randomLetters[i][j] = alphabet[number];
                            }
                        }
                    }                    
                }                
            }            
                    
            this.pos.x = 0;
            this.pos.y = 0;
            this.size.x = this.columns * this.letterSize.x;
            this.size.y = this.lines * this.letterSize.y;        
        },

        update: function()
        {
            this.parent();
            if (this.wasClicked() == true)
            {
                var clickedColumn = Math.floor(ig.input.mouse.x / this.letterSize.y);
                var clickedLine = Math.floor(ig.input.mouse.y / this.letterSize.y);
                
                ig.global.c = clickedColumn;
                ig.global.l = clickedLine;
                
                for (var i = 0; i < this.words.length; ++i)
                {
                    this.words[i].checkClickedLetter(clickedColumn, clickedLine);
                }
            }
        },
        
        draw: function()
        {
            var locX = 10;
            var locY = 10;
            
            var offsetX = 40;
            var offsetY = 40;

	    for (var i = 0; i < this.words.length; ++i)
	    {               
                locX = 13;
                locX += this.words[i].column * offsetX;

                locY = 10;
                locY += this.words[i].line * offsetY;
                
                switch (this.words[i].overlayType)
                {
                    case 'red':                     
                            this.words[i].draw(this.redOverlay, this.letterSize.x, this.letterSize.y);
                            break;
                        
                    case 'green':                     
                            this.words[i].draw(this.greenOverlay, this.letterSize.x, this.letterSize.y);
                            break;
                        
                    case 'blue':                     
                            this.words[i].draw(this.blueOverlay, this.letterSize.x, this.letterSize.y);
                            break;
                        
                    case 'yellow':                     
                            this.words[i].draw(this.yellowOverlay, this.letterSize.x, this.letterSize.y);
                            break;
                        
                    case 'pink':                     
                            this.words[i].draw(this.pinkOverlay, this.letterSize.x, this.letterSize.y);
                            break;
                        
                    case 'orange':                     
                            this.words[i].draw(this.orangeOverlay, this.letterSize.x, this.letterSize.y);
                            break;                                            
                }
                
                for (var j = 0; j < this.words[i].word.length; ++j)
                {
                    this.font.draw( '' + this.words[i].word[j], locX, locY, ig.Font.ALIGN.LEFT );
                    
                    if (this.words[i].vertical == true)
                    {
                       locY += offsetY;
                    }
                    else
                    {
                       locX += offsetX;
                    }
                }
                
                var rightColumn = 490;
                var rightLine = 20 + (16 * i);
                
                this.font.draw( this.words[i].word, rightColumn, rightLine, ig.Font.ALIGN.LEFT );
                
                if (this.words[i].isSolved() == true)
                {
                    this.ok.draw( rightColumn + this.font.widthForString(this.words[i].word) + 2, rightLine );
                }
	    }
            
            var letterY = 10;         
            for (var i = 0; i < this.lines; ++i)
            {
                var letterX = 10;
                for (var j = 0; j < this.columns; ++ j)
                {
                    if (this.randomLetters[j][i] != '#')
                    {
                        this.font.draw( '' + this.randomLetters[j][i], letterX, letterY, ig.Font.ALIGN.LEFT );
                    }
                    
                    letterX += offsetX;
                }
                
                letterY += offsetY;
            }            
        }
    });
});